package user

import (
	"context"

	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userservice"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserLevelSearchLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 会员管理 - 会员层级/查询
func NewUserLevelSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLevelSearchLogic {
	return &UserLevelSearchLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserLevelSearchLogic) UserLevelSearch(req *types.UserLevelSearchRequest) (resp *types.UserLevelSearchResponse, err error) {
	rpcReq := &userservice.UserLevelSearchRequest{
		Id:                req.Id,
		AgentLevel:        req.AgentLevel,
		Level:             req.Level,
		RegisterTimeStart: req.RegisterTimeStart,
		RegisterTimeEnd:   req.RegisterTimeEnd,
		Page:              req.Page,
		PageSize:          req.PageSize,
	}
	searchResp, err := l.svcCtx.UserServiceRPC.UserLevelSearch(l.ctx, rpcReq)
	if err != nil {
		logc.Error(l.ctx, "UserLevelSearch RPC error: %v", err)
		return &types.UserLevelSearchResponse{
			Pagination: types.Pagination{},
			UserList:   []*types.UserLevel{},
		}, nil
	}

	userList := make([]*types.UserLevel, len(searchResp.UserList))
	for index, item := range searchResp.UserList {
		userList[index] = &types.UserLevel{
			Id:                          item.Id,
			Account:                     item.Account,
			AgentLevel:                  item.AgentLevel,
			Level:                       item.Level,
			ChildNum:                    item.ChildNum,
			AllChildNum:                 item.AllChildNum,
			ParentId:                    item.ParentId,
			RegistAt:                    item.RegistAt,
			BetAmount:                   item.BetAmount,
			BetCount:                    item.BetCount,
			BetUserCount:                item.BetUserCount, // todo
			WinAmount:                   item.WinAmount,
			WinCount:                    item.WinCount,     // todo
			WinUserCount:                item.WinUserCount, // todo
			RechargeAmount:              item.RechargeAmount,
			RechargeCount:               item.RechargeCount,
			RechargeUserCount:           item.RechargeUserCount, // todo
			WithdrawAmount:              item.WithdrawAmount,
			WithdrawCount:               item.WithdrawCount,
			WithdrawUserCount:           item.WithdrawUserCount, // todo
			BrokerAmount:                item.BrokerAmount,
			RedAmount:                   item.RedAmount,
			SinginAmount:                item.SinginAmount,
			AgentRedAmount:              item.AgentRedAmount,
			RechargeGiftAmount:          item.RechargeGiftAmount,
			RegistGiftAmount:            item.RegistGiftAmount,
			BetRechargeAmount:           item.BetRechargeAmount,
			FirstRechargeFullGiftAmount: item.FirstRechargeFullGiftAmount,
		}
	}

	resp = &types.UserLevelSearchResponse{
		Pagination: types.Pagination{
			Page:      searchResp.Pagination.Page,
			PageSize:  searchResp.Pagination.PageSize,
			Total:     searchResp.Pagination.Total,
			TotalPage: searchResp.Pagination.TotalPage,
		},
		UserList: userList,
	}

	return
}
